/**
* 
* report system global
* 
* Copyright (c) 2009 Ecole Centrale de Nantes IRCCyN  
* 
* see <a href="{@docRoot}/Documentation/report.html"> for more information</a>
* 
* @author: Urunuela Richard
* @version 3.2
*/
package report;

import java.awt.Color;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

import programme.GUI.JConsole;
import storm.SimulationKernel;
import storm.Tasks.Task;

public class report {
	String command="";
	//SimulationKernel kernel;
	JConsole console;
	ArrayList <sonde>Sondes;
	sonde Courante;
	output theOutPut;
	String fileCommand;
	String fileOut;
	public report(String nodeValue) {
		this.command = nodeValue;
		Sondes=new ArrayList<sonde>();
		// TODO Auto-generated constructor stub
	}
	/**
	 * 
	 * @param console2
	 * @param f
	 * @param out
	 */
	public report(JConsole console2, String f, String out) {
		this.console = console2;
		Sondes=new ArrayList<sonde>();
		console.append(Color.blue, "\n -----------------------------------------------------------");
		console.append(Color.blue, "\n Processing report from file " + f  + " in " + out);
		console.append(Color.blue, "\n -----------------------------------------------------------");
		this.fileCommand = f;
		this.fileOut = out;
		if (out.contains(".pdf")){
			this.theOutPut = new pdfReport(out);
			console.append(Color.blue, "\n Generate PDF ");
		}
		else if(out.contains(".txt")){
			this.theOutPut = new txtReport(out);
			console.append(Color.blue, "\n Generate txt ");
		}
		else {
			this.theOutPut = new guiReport(this.console);
			console.append(Color.blue, "\n GUI ");
		}
		// TODO Auto-generated constructor stub
	}
	/**
	 * Opne command report file
	 * @param f
	 */
	private void opencommand(String f) {
		InputStreamReader flog	= null;
		LineNumberReader llog	= null;
		String myLine		     = null;
		try{ 
			flog = new InputStreamReader(new FileInputStream(f) );
			llog = new LineNumberReader(flog);
			while ((myLine = llog.readLine()) != null) { 
	                      // --- Affichage de la ligne lu depuis le fichier
						this.command = this.command + myLine +"\n";

	                }
	        }catch (Exception e){
	               // --- Gestion erreur lecture du fichier (fichier non existant, illisible, etc.)
	               System.err.println("Error : "+e.getMessage());
	        }

		
	}
	/**
	 * process command 
	 * @param console
	 * @return
	 */
	public boolean process(JConsole console){
		this.console = console;
		console.append(Color.blue, "\n Open Command file "+ this.fileCommand + "\n");
		this.opencommand(this.fileCommand);
		
		this.parse();

		console.append(Color.black, "\n##> ");
		this.theOutPut.close();
		return true;
		}
	/**
	 * parse command
	 */
	private void parse() {
		StringTokenizer listecommande = new StringTokenizer(command,"\n");
		
		while (listecommande.hasMoreElements()){
			String ligne = listecommande.nextToken() ;
			ligne=ligne.trim();
			

			
			if (ligne.contains("[")) {
				StringTokenizer item = new StringTokenizer(ligne,"[");

				String name =item.nextToken(); 
				this.createSonde(name);
				
				
			
			}
			else if (ligne.contains("]")) {
				Courante=new sonde();
			}
			else {
				Courante.parse(ligne);
			}
			
		}
		
	}
	/***
	 * Create sonde for each [ NAME 
	 * @param name
	 */
	private void createSonde(String name) {
		// TODO Auto-generated method stub
		if (name.compareToIgnoreCase("scheduler")==0) {
			Courante=new  scheduler(this.theOutPut,this.console.getKernel().getScheduler());
			this.Sondes.add(Courante);
			
		}
		if (name.compareToIgnoreCase("tasks")==0) {
			Courante=new  tasks(this.theOutPut,this.console.getKernel().getTasksListeManager().getALLTask());
			this.Sondes.add(Courante);
			
		}
		if (name.compareToIgnoreCase("system")==0) {
			Courante=new  system(this.theOutPut,this.console.getKernel());
			this.Sondes.add(Courante);
			
		}
		if (name.compareToIgnoreCase("processors")==0) {
			Courante=new  processors(this.theOutPut,this.console.getKernel());
			this.Sondes.add(Courante);
			
		}
		if (name.contains("task") && (!name.contains("tasks"))) {
						StringTokenizer listeparam = new StringTokenizer(name,"task");
					    String nums = listeparam.nextToken();
					 //System.out.println(nums + "-- " + nums.getClass()); 
					  	int num = Integer.valueOf(nums.trim());
					//Task ref = this.kernel.getTasksListeManager().getTaskbyId(num);
					  	Task ref = this.console.getKernel().getTasksListeManager().getTaskbyId(num);
					  	if (ref == null){
					  	               this.console.afficheError("Task  not know with id[ "+ num +"  ] in report file ");
					  	              
					  	}
					  	              Courante=new  task(theOutPut, ref,this.console);
					  	             this.Sondes.add(Courante);
			//		Courante=new  tasks(this.theOutPut,this.console.getKernel().getTasksListeManager().getALLTask());
	//		this.Sondes.add(Courante);
			
		}
		
	}
}

